package com.tcsl.mylibrary;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.tcsl.mylibrary.utils.DecimalUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * 描述:拉卡拉支付
 * <p/>作者：wjx
 * <p/>创建时间: 2017/5/26 16:44
 */
public class PayLaKaLa extends TcslPay {
    private static final int PAY = 0x6eff;

    public PayLaKaLa(Context context) {
        super(context);
    }

    @Override
    protected void pay(Float money, String orderNo, String merchantNo) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddhhmmss");
        Date curDate = new Date(System.currentTimeMillis());
        String time = formatter.format(curDate);
        try {
            Intent intent = new Intent();
            ComponentName componet = new ComponentName(
                    "com.lkl.cloudpos.payment",
                    "com.lkl.cloudpos.payment.activity.MainMenuActivity");
            Bundle b = new Bundle();
            // 交易类型 00－消费类 01-授权类
            b.putString("proc_tp", "00");
            // 支付方式
            b.putString("pay_tp", "0");
            // 交易金额
            b.putString("amt", String.valueOf(DecimalUtil.pointZZ(money)));
            // 报文类型 固定值，只区分请求(0200)和应答(0210)．
            b.putString("msg_tp", "0200");
            // 交易处理码 000000 消费
            b.putString("proc_cd", "000000");
            // 订单号
            b.putString("order_no", orderNo + "_" + time);
            // 调用者应用包名
            b.putString("appid", pkName);
            // 交易时间戳
            b.putString("time_stamp", time);
            // 订单信息
            b.putString("order_info", "天财商龙吾享智能POS手机点餐");
            // 扩展参数 可选非必须
            // b.putString("reserve", "");
            intent.setComponent(componet);
            intent.putExtras(b);
            mActivity.startActivityForResult(intent, PAY);
        } catch (Exception e) {
            mListener.notSupport();
        }
    }

    @Override
    public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PAY) {
            switch (resultCode) {
                case Activity.RESULT_OK:
                    mListener.paySuccess("拉卡拉支付成功", "");
                    break;
                case Activity.RESULT_CANCELED:
                    try {
                        Bundle bundle = data.getExtras();
                        String reason = bundle.getString("reason");
                        if (!TextUtils.isEmpty(reason)) {
                            mListener.payError(reason);
                        } else {
                            mListener.payError("拉卡拉支付取消");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case -2:
                    Bundle bundle2 = data.getExtras();
                    String reason2 = bundle2.getString("reason");
                    if (!TextUtils.isEmpty(reason2)) {
                        mListener.payError(reason2);
                    } else {
                        mListener.payError("拉卡拉支付取消");
                    }
                    break;
                default:
                    mListener.payError("未知错误");
            }
            return true;
        } else {
            return false;
        }
    }
}
